
#include <string>

#include <QApplication>  // NOLINT: cpplint is unable to handle the include order here
#include <QHBoxLayout>  // NOLINT: cpplint is unable to handle the include order here
#include <QInputDialog>  // NOLINT: cpplint is unable to handle the include order here
#include <QProgressDialog> // NOLINT: cpplint is unable to handle the include order here
#include <QPushButton>  // NOLINT: cpplint is unable to handle the include order here
#include <QTimer>  // NOLINT: cpplint is unable to handle the include order here
#include <QVBoxLayout>  // NOLINT: cpplint is unable to handle the include order here

#include <rviz_common/display.hpp>
#include <rviz_common/properties/property.hpp>
#include <rviz_common/properties/property_tree_widget.hpp>
#include <rviz_common/properties/property_tree_with_help.hpp>
#include <rviz_common/ros_integration/ros_node_abstraction_iface.hpp>
#include <rviz_common/visualization_manager.hpp>

#include "rviz_custom_plugins/panels/custom_displays_panel.h"


using rviz_common::Display;


namespace rviz_custom_plugins
{

namespace panels
{

CustomDisplaysPanel::CustomDisplaysPanel(
  QWidget * parent)
: Panel(parent)
{
  setObjectName("Displays/CustomDisplaysPanel");

  propertyTree = new PropertyTreeWidget;
  propertyTree->setObjectName("CustomDisplaysPanel/PropertyTree");

  auto layout = new QVBoxLayout;
  layout->setContentsMargins(0, 0, 0, 2);
  layout->addWidget(propertyTree);
  // layout->addLayout(button_layout);

  setLayout(layout);

  widgetManager = WidgetManager::getInstance();

  // connect(add_button, SIGNAL(clicked(bool)), this, SLOT(onNewDisplay()));
  // connect(duplicate_button_, SIGNAL(clicked(bool)), this, SLOT(onDuplicateDisplay()));
  // connect(remove_button_, SIGNAL(clicked(bool)), this, SLOT(onDeleteDisplay()));
  // connect(rename_button_, SIGNAL(clicked(bool)), this, SLOT(onRenameDisplay()));
  // connect(propertyTree, SIGNAL(selectionHasChanged()), this, SLOT(onSelectionChanged()));
}

void CustomDisplaysPanel::onInitialize()
{
    vis_manager_ = dynamic_cast<VisualizationManager*>(getDisplayContext());
    propertyTree->setModel(vis_manager_->getDisplayTreeModel());
    connect(widgetManager, &WidgetManager::widgetShow, this, &CustomDisplaysPanel::handleWidgetShow);
    // setEnabled(false);
    // parentWidget()->hide();
}

void CustomDisplaysPanel::save(Config config) const
{
  // Panel::save(config);
  propertyTree->save(config);

}

void CustomDisplaysPanel::load(const Config & config)
{
  // Panel::load(config);
  propertyTree->load(config);
}


void CustomDisplaysPanel::handleWidgetShow(int widgetId, bool isShow) {
    if(widgetId == WidgetManager::DISPLAY_PROPERTY_PANEL) {
        if (isShow) {
            setEnabled(true);
            parentWidget()->show();
        } else {
            setEnabled(false);
            parentWidget()->hide();
        }
    }
}

}

}

#include <pluginlib/class_list_macros.hpp>
PLUGINLIB_EXPORT_CLASS(rviz_custom_plugins::panels::CustomDisplaysPanel,rviz_common::Panel )
